package com.lottery.controller.pc28.lobbyController;

import com.alibaba.fastjson.JSONObject;
import com.lottery.dto.ResultDto;
import com.lottery.dto.SscOddsShow;
import com.lottery.service.pc28.lobbyService.LotteryOddsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/Odd")
public class OddController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LotteryOddsService lotteryOddsService;
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value="/show", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public ResultDto<Map<String, List<SscOddsShow>>> selectLotteryOddsSshow(HttpServletRequest request){
        ResultDto<Map<String, List<SscOddsShow>>> result= new ResultDto<>();
        String body = "";
        try {
            body = readHttpBody(request);

            JSONObject obj = JSONObject.parseObject(body);

            int lotteryType = obj.getInteger("lotteryType");
            result.setData(lotteryOddsService.selectLotteryOddsSshow(lotteryType));
            result.setSuccess(true);
            result.setMessage("拉取成功");
            result.setResult(lotteryType);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("*****playWay error******:", e);
            result.setSuccess(false);
            result.setMessage("拉取失败");
        }
        return result;
    }


    /**
     * 解析json
     *
     * @param request
     * @return
     * @throws Exception
     */
    private String readHttpBody(HttpServletRequest request) throws Exception {

        // 读取input流
		/*
		 * BufferedReader br = new BufferedReader( new
		 * InputStreamReader((ServletInputStream) request.getInputStream(),
		 * "utf-8")); StringBuffer sb = new StringBuffer(""); String temp; while
		 * ((temp = br.readLine()) != null) { sb.append(temp); } br.close();
		 * return sb.toString();
		 */
        String attribute = (String) request.getAttribute("json");
        return attribute;
    }


}
